
function openModal() {
    document.getElementById('itemModal').style.display = 'flex';
    document.getElementById('itemImage').value = '';
    const preview = document.getElementById('preview');
    preview.src = '#';
    preview.style.display = 'none';
    document.getElementById('itemName').value = '';
    document.getElementById('found_person_name').value = '';
    document.getElementById('itemDescription').value = '';
    document.getElementById('itemDate').value = '';
    document.getElementById('itemTime').value = '';

}

function closeModal() {
    document.getElementById('itemModal').style.display = 'none';
}

function addItem() {
    const itemName = document.getElementById('itemName').value;
    found_person_name = document.getElementById('found_person_name').value;
    const itemDescription = document.getElementById('itemDescription').value;
    const itemDate = document.getElementById('itemDate').value;
    itemTime = document.getElementById('itemTime').value;
    const itemImage = document.getElementById('itemImage').files[0];
    if (!found_person_name) {
        found_person_name = 'system';
    }
    if (!itemTime) {
        itemTime = '12:00:00.000';
    }
    if (itemName && itemDate && itemImage && itemDescription) {
        const formData = new FormData();
        formData.append('itemName', itemName);
        formData.append('found_person_name', found_person_name);
        formData.append('itemDescription', itemDescription);
        formData.append('itemDate', itemDate);
        formData.append('itemTime', itemTime);
        formData.append('itemImage', itemImage);
        fetch('/Lost_found_pic_upload', {
            method: 'POST',
            body: formData,
        }).then(response => response.text())
            .then(data => {
                if (data == "error") {
                    alert('添加物品失败，请检查网络连接或联系管理员。');
                }
                if (data == "success") {
                    const reader = new FileReader();
                    reader.onload = function (e) {
                        const lostAndFound = document.querySelector('.lost-and-found');
                        const newItem = document.createElement('div');
                        newItem.classList.add('item-card');
                        newItem.innerHTML = `
                        <img src="${e.target.result}" alt="${itemName}">
                         <!-- <button class="delete-button">删除</button> -->
                        <div class="item-description">
                            <div class="item-name">${itemName}</div>
                            <div>捡到人:${ found_person_name }</div>
                            <div>捡到时间: ${itemDate} ${' '+itemTime}</div>
                            <div class="item-introduction-popup">
                                描述信息：${itemDescription}
                            </div>
                        </div>
                    `;
                        lostAndFound.appendChild(newItem);
                        closeModal();
                    };
                    reader.readAsDataURL(itemImage);
                }
            })
            .catch(error => {
                alert('操作失败，请检查网络连接或联系管理员。\nError:', error);
            });
    } else {
        if (!itemName) {
            alert("请填写物品名称。");
        }
        if (!itemDate) {
            alert("请填写捡到时间。");
        }
        if (!itemImage) {
            alert("请上传图片。");
        }
        if (!itemDescription) {
            alert("请填写物品描述。");
        }
    }
}


function previewImage(event) {
    const preview = document.getElementById('preview');
    const file = event.target.files[0];
    const reader = new FileReader();

    reader.onload = function () {
        preview.src = reader.result;
        preview.style.display = 'block'; // 显示图片预览
    }

    if (file) {
        reader.readAsDataURL(file);
    }
}